package com.ocom.payconfig.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.entity.payconfig.EntityDishesInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/12/30
 **/

@Slf4j
@Service
public class DishesCommonServiceImpl implements DishesCommonService {

	@Autowired
	private DishesInfoService dishesInfoService;

	@Autowired
	private XfMerchantInfoService xfMerchantInfoService;

	@Autowired
	private XfPointInfoService xfPointInfoService;

	@Autowired
	private XfDeviceInfoService xfDeviceInfoService;


	@Override
	public Result getDishesMerchByPoint(Long comId, Integer pointId) {
		Map<String,Integer> map =new HashMap<>();
		map.put("dishesMerchId",0);
		if(pointId==0){
			log.info("获取消费点商户1 【{}】【{}】【{}】",comId,pointId,map);
			return ResultUtil.success(map);
		}
		EntityXfPointInfo entityXfPointInfo = xfPointInfoService.getTollPointInfo(comId,pointId);
		if(entityXfPointInfo == null){
			log.info("获取消费点商户2 【{}】【{}】【{}】",comId,pointId,map);
			return ResultUtil.error(ResultEnum.Err_3044);
		}
		if(entityXfPointInfo.getMerchId()==null || entityXfPointInfo.getMerchId()==0){
			return ResultUtil.success(map);
		}

		List<EntityDishesInfo> list =  dishesInfoService.selectDishesInfoList(comId);
		if(list==null || list.size()==0){
			log.info("获取消费点商户3 【{}】【{}】【{}】",comId,pointId,map);
			return ResultUtil.success(map);
		}


		List<EntityXfMerchantInfo>  merchantInfoList = xfMerchantInfoService.GetMerchInfo(comId);

		if(merchantInfoList==null || merchantInfoList.size()==0){
			log.info("获取消费点商户3 【{}】【{}】【{}】",comId,pointId,map);
			return ResultUtil.success(map);
		}

		Integer merchId = getParentMerchId(merchantInfoList,entityXfPointInfo.getMerchId());
		log.info("获取消费点商户5 【{}】【{}】【{}】",merchantInfoList,entityXfPointInfo,merchId);

		map.put("dishesMerchId",merchId);

		log.info("获取消费点商户6 【{}】【{}】【{}】",comId,pointId,map);
		return ResultUtil.success(map);
	}

	@Override
	public Result resetMachDishesWhite(Long comId, Integer pointId, String deviceNo) {
		Result result = getDishesMerchByPoint(comId,pointId);
		if(result.getCode()!=200){
			return result;
		}
		Map<String,Integer> map = JSON.parseObject(JSON.toJSONString(result.getData()),Map.class);
		Boolean boo = xfDeviceInfoService.updateMenu(comId,deviceNo,
				1,
				-1,
				-1,
				-1,
				map.get("dishesMerchId"));
		return ResultUtil.success();
	}

	@Override
	public Result resetPointDishesWhite(Long comId, Integer pointId) {
		Result result = getDishesMerchByPoint(comId,pointId);
		if(result.getCode()!=200){
			return result;
		}
		Map<String,Integer> map = JSON.parseObject(JSON.toJSONString(result.getData()),Map.class);
		Boolean boo = xfDeviceInfoService.updateMenu(comId,pointId,
				1,
				-1,
				-1,
				-1,
				map.get("dishesMerchId"));
		return ResultUtil.success();
	}


	//往上递归
	Integer getParentMerchId(List<EntityXfMerchantInfo> list ,Integer merchId){
		Boolean flag =false;
		for (EntityXfMerchantInfo entityXfMerchantInfo : list){
			//找到当前
			if(entityXfMerchantInfo.getMerchId().equals(merchId)){
				if(entityXfMerchantInfo.getParentMerchId()!=0){
					merchId= entityXfMerchantInfo.getParentMerchId();
					flag=true;
				}
			}
		}
		if(flag){
			return getParentMerchId(list,merchId);
		}
		return merchId;
	}


}
